﻿using CPPEI;
using CPPEI.WebApi;
using SLG.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLG.WebApi.User
{
    [PostAction("User", "Login", "1.0")]
    public class LoginAction : BaseAction<LoginParams, LoginResult>
    {
        protected override ActionResult<LoginResult> OnExecute(ActionContext context, SysParameters sysParams, LoginParams opParams)
        {
            UserService service = new UserService();
            SingleResult<Domain.Entities.SYS_User> serResult = service.Get(opParams.Account, opParams.Password);
            if(serResult.IsSuccess)
            {
                CPPEI.WebApi.Intergration.Config.AuthProvider.Save(serResult.Data.Id.ToString(), TimeSpan.FromHours(1));

                LoginResult actResult = new LoginResult();
                actResult.Id = serResult.Data.Id;
                return new ActionResult<LoginResult>(actResult);
            }
            else
            {
                return new ActionResult<LoginResult>(serResult);
            }
        }
    }

    public class LoginParams
    {
        public string Account { get; set; }

        public string Password { get; set; }
    }

    public class LoginResult
    {
        public int Id { get; set; }
    }
}
